<template>
    <div>
        <el-card>
            <el-row>
                <!-- 添加协议 -->
                <el-col :span="6">
                    <el-button @click="protocolDialogVisible = true"><i class="el-icon-plus"></i>添加协议</el-button>
                </el-col>
            </el-row>
            <!-- 表格 -->
            <el-table :data="searchProtocolList" style="width: 100%" class="opinion" :header-cell-style="{background:'#eef1f6',color:'#606266'}">
                <el-table-column label="协议类型" prop="type" align="center"></el-table-column>
                <el-table-column label="协议名称" prop="name" align="center"> </el-table-column>
                <el-table-column label="状态" prop="inUse" align="center">
                    <template slot-scope="scope">
                            <p :class="{inuse:(scope.row.inUse)}">{{ (scope.row.inUse)?'启用':'停用' }}</p>
                        </template>
                    </el-table-column>
                <el-table-column label="操作" align="center" min-width="150">
                    <template slot-scope="scope">
                        <el-button size="mini" type="primary" @click="preView(scope.$index, scope.row)">预览</el-button>
                        <el-button size="mini" type="danger" @click="inUseHandler(scope.$index, scope.row)"><i class="el-icon-edit"></i>{{(scope.row.inUse)?'停用':'启用' }}</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页 -->
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="searchList.page"
                :page-sizes="[5, 10, 15, 20]"
                :page-size="searchList.size"
                layout="total, sizes, prev, pager, next, jumper"
                :total=count
                align="center"
                small
                class="pagenation">
            </el-pagination>
            <!-- 启用状态模态框 -->
            <el-dialog
                title="提示"
                :visible.sync="inUseDialogVisible"
                width="30%"
                >
                <span>确认启用或者停用？</span>
                <span slot="footer" class="dialog-footer">
                <el-button @click="inUseDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="sureHandler">确 定</el-button>
                </span>
            </el-dialog>
            <!-- 添加协议模态框 -->
            <el-dialog title="新增协议" :visible.sync="protocolDialogVisible" width="800px" center>
                <!-- 内容主题部分 -->
                <el-row :gutter="20">
                    <el-col :span="16" :offset="4">
                        <el-form :model="protocolForm" ref='protocolForm' label-width="90px" label-position="right">
                            <!--协议名称 -->
                            <el-form-item label="协议名称">
                                <el-input v-model="protocolForm.name" prop="name"></el-input>
                            </el-form-item>
                            <!--协议类型 -->
                            <el-form-item label="协议类型" prop="type">
                                <el-select v-model="protocolForm.type">
                                    <el-option
                                        v-for="item in types"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <!-- 状态 -->
                            <el-form-item label="状态" prop="inUse">
                                <el-select v-model="protocolForm.inUse">
                                    <el-option
                                        v-for="item in addUseStatus"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <!-- 协议内容 -->
                            <el-form-item label="协议内容"  >
                                <el-input type="textarea" :rows="8" v-model="protocolForm.context" prop="context"></el-input>
                            </el-form-item>
                            
                        </el-form>
                    </el-col>
                </el-row>
                <!-- 编辑模态框 -->
                <div slot="footer" class="dialog-footer">
                    <el-button @click="protocolDialogVisible = false">返回</el-button>
                    <el-button type="primary" @click="submmitProtocolForm('protocolForm')">
                        确 定
                    </el-button>
                </div>
            </el-dialog>
            <!-- 预览协议模态框 -->
            <el-dialog title="预览协议" :visible.sync="preViewDialogVisible" width="800px" center>
                <!-- 内容主题部分 -->
                <el-row :gutter="20">
                    <el-col :span="16" :offset="4">
                        <el-form :model="preViewForm" label-width="90px" label-position="right" disabled>
                            <!--协议名称 -->
                            <el-form-item label="协议名称">
                                <el-input v-model="preViewForm.name" prop="name"></el-input>
                            </el-form-item>
                            <!--协议类型 -->
                            <el-form-item label="协议类型">
                                <el-select v-model="protocolForm.type">
                                    <el-option
                                        v-for="item in types"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <!-- 状态 -->
                            <el-form-item label="状态" prop="inUse">
                                <el-select v-model="preViewForm.inUse">
                                    <el-option
                                        v-for="item in addUseStatus"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <!-- 协议内容 -->
                            <el-form-item label="协议内容">
                                <el-input type="textarea" :rows="8" v-model="preViewForm.context" prop="context"></el-input>
                            </el-form-item>
                        </el-form>
                    </el-col>
                </el-row>
                <!-- 编辑模态框 -->
                <div slot="footer" class="dialog-footer">
                    <el-button @click="preViewDialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="preViewDialogVisible = false">
                        确 定
                    </el-button>
                </div>
            </el-dialog>
        </el-card>
    </div>
</template>

<script>
import {getHandler4,insertHandler,startHandler} from '../../http/content'
    export default {
        name: "Protocol",
        data(){
            return {
                //数据总数
                count:null,
                //启用模态框状态码
                inUseDialogVisible:false,
                //预览模态框状态码
                preViewDialogVisible:false,
                //新增协议模态框状态码
                protocolDialogVisible:false,
                //添加时启用状态
                addUseStatus: [
                    {
                        value: true,
                        label: "启用",
                    },
                    {
                        value: false,
                        label: "停用",
                    },
                ],
                //协议类型
                types: [
                    {
                        value: "用户注册协议",
                        label: "用户注册协议",
                    },
                    {
                        value: "借款通用协议",
                        label: "借款通用协议",
                    },
                    {
                        value: "银行卡绑定协议",
                        label: "银行卡绑定协议",
                    },
                    {
                        value: "担保标借款协议",
                        label: "担保标借款协议",
                    },
                    {
                        value: "信用标借款协议",
                        label: "信用标借款协议",
                    }
                ],
                //版本搜索参数
                searchList:{
                    page:1,
                    size:5,
                },
                //获取的版本列表
                searchProtocolList:[],
                getProtocolList:[
                    {
                        name:"1",
                        type:1,
                        inUse:true
                    },
                    {
                        name:"2",
                        type:2,
                        inUse:true
                    }
                ],
                //当前行id
                nowId:null,
                //启用表单
                startForm:{},
                //协议提交表单
                protocolForm:{
                    name:"",
                    type:"信用标借款协议",
                    inUse:true,
                    context:''
                },
                //预览协议表单
                preViewForm:{
                    name:"",
                    type:"",
                    inUse:true,
                    context:""
                }

            }
        },
        created(){
            this.searchProtocol();
        },
        watch:{
            'searchList.inUse':function(){
                this.searchList.page = 1;
                this.searchProtocol();
            }
        },
        methods:{
            //搜索版本
            searchProtocol(){
                let path = '/agreement/selectAllAndEffective';
                getHandler4(path,this.searchList)
                    .then(res => {
                        console.log(res,'获取协议数据');
                        this.searchProtocolList = res.data.data.result.agreements;
                        this.count = res.data.data.result.count;
                    })
                    .catch(err => {
                        console.log('请求失败', err);
                    })    
            },
            // 分页大小改变
            handleSizeChange(newSize) {
                this.searchList.size = newSize;
                this.searchList.page = 1;
                console.log(this.searchVersionList,'分页大小改变请求参数');
                this.searchProtocol();
            },
            //当前页改变
            handleCurrentChange(newSize) {
                this.searchList.page = newSize;
                console.log(this.searchList.page,'当前页改变请求参数');
                this.searchProtocol();
            },
            //停用处理处理
            inUseHandler(index,row){
                this.inUseDialogVisible = true;
                this.startForm = row;
                this.startForm.inUse = this.startForm.inUse ? 0:1;
                console.log(this.startForm,'启用表单');
            },
            //确认启用停用
            sureHandler(){
                let path = '/agreement/updateinUse';
                startHandler(path,this.startForm)
                .then(res => {
                    if(res.data.code != 20000) {
                        this.nowId = null;
                        return this.$message.error(res.data.message);
                    }
                    this.$message.success('更改成功');
                    this.nowId = null;
                    this.inUseDialogVisible = false;
                })
                .catch(err => {
                    this.$message.error(err);
                })

            },
            //提交新版本
            submmitProtocolForm(formName){
                console.log(this.protocolForm,'提交的表单');
                this.$refs[formName].validate((valid) => {
                    let path = '/agreement/insert';
                    if (valid) {
                        insertHandler(path,this.protocolForm)
                            .then((res) => {
                            if(res.data.code != 20000){
                                return this.$message.error(res.data.message);
                            }
                            this.$message.success('添加成功');
                            this.protocolDialogVisible = false;
                            this.$router.push('/content/protocol');
                            })
                            .catch((err) => {
                                this.$message.error(err);
                            });
                        } else {
                        this.$message.error('提交内容不能为空');
                        return false;
                    }
                });
            },
            //预览
            preView(index,row){
                this.preViewDialogVisible = true;
                this.preViewForm = row;
                console.log(row,12121212);
            }
        }
    }
</script>

<style scoped lang="less">
.el-table{
  margin-top: 20px;
}
.inuse {
  color: #009900;
}
.textarea{
    overflow: auto;
    height: 300px;
}
// 分页
.pagenation{
    margin-top: 20px;
}
</style>